package com.banyuan.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;

@Component
@Aspect
public class PowerAop {

    //@Pointcut("execution(* com.banyuan.controller.*.*(..))")//com.banyuan.controller包下所有类，所有方法增强
    //@Pointcut("@target(com.banyuan.annotation.MyAnnoClass)")//只给标注了MyAnnoClass注解的类增强
    @Pointcut("@annotation(com.banyuan.annotation.MyAnnotationMethod)")//只给标注了MyAnnoMethod注解的方法增强
    public void pointcut() {

    }


    @Before( "pointcut()")
    public void before(JoinPoint joinPoint) {
        Date date = new Date();
        Object[] args = joinPoint.getArgs(); // 获取参数
        Signature signature = joinPoint.getSignature(); // 获取执行的方法
        Object target = joinPoint.getTarget();// 获取目标类


        System.out.println(date + "访问了" + target.getClass().getName() + "类，调用了" + signature.getName() +"方法，参数是" + Arrays.toString(args)  );
    }


    @After("pointcut()")
    public void after(){
        System.out.println("after");
    }

    @AfterReturning("pointcut()")
    public void afterReturning(){
        System.out.println("afterReturning");
    }

    @AfterThrowing("pointcut()")
    public void afterThrowing(){
        System.out.println("afterThrowing");
    }

    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("进入方法前");

        Object[] args = proceedingJoinPoint.getArgs();
        System.out.println(Arrays.toString(args));
        proceedingJoinPoint.proceed();

        System.out.println("进入方法后");
    }
}
